<?php
/**
 * Created by PhpStorm.
 * User: Xzareth
 * Date: 20/05/14
 * Time: 9:33 PM
 */
require 'init.php';

if (isset($_POST['submit'])) {

    $isAdmin = 0;
    if (isset($_REQUEST['isAdmin'])) $isAdmin = 1;

    $userID = $_REQUEST['userID'];
    $email = $_REQUEST['email'];
    $firstName = $_REQUEST['fname'];
    $lastName = $_REQUEST['lname'];

    $insertCustomerFormStatus = array();
    $modifyFormStatus = array();

    if ($_REQUEST['submit'] == "X") {
        $modifyFormStatus[] = $user->deleteUser($userID);
    }
    if ($_REQUEST['submit'] == "Insert") {
        $insertCustomerFormStatus[] = ($user->register($email, $firstName, $lastName, $password)) ? 'Successfully added account' : 'Unable to add account';

    } else if ($_REQUEST['submit'] == "Update") {
        $modifyFormStatus[] = $user->updateUser($userID, $email, $firstName, $lastName, $isAdmin);
    }
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script src="javascripts/validateForm.js"></script>
    <title>Customers</title>
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <div id="adminDashboard">
            <div id="returnToDashboard">
                <a href="admin.php"><h3>Return to Dashboard</h3></a>
            </div>
            <?php include "customersDashboard.php"; ?>
            <div id="currentCustomers">
                <h3>All Current Customers</h3>
            </div>
            <div id="customerResults">
                <?php
                $rowNumber = 0;

                if (isset($_GET['customerResults'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('customerResults').style.display='block';
                            document.getElementById('modifyFormContainer').style.display='none';
                            </script>";
                }

                if (isset($_GET['customerSurname'])) {
                    $sql = "SELECT * FROM Users WHERE LastName = '$_GET[customerSurname]'";
                } else {
                    $sql = "SELECT * FROM Users";
                }

                foreach ($db->query($sql) as $row) {
                    $rowNumber++;
                    ?>
                    <table>
                        <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                        <tr>
                            <th class="userIDCol">ID</th>
                            <th class="emailCol">Email</th>
                            <th class="fNameCol">Name</th>
                            <th class="lNameCol">Surname</th>
                            <th class="dateJoinedCol">Date Joined</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td class="userIDCol"><?php echo $row['UserID']; ?></td>
                            <td class="emailCol"><?php echo $row['Email']; ?></td>
                            <td class="fNameCol"><?php echo $row['FirstName']; ?></td>
                            <td class="lNameCol"><?php echo $row['LastName']; ?></td>
                            <td class="dateJoinedCol"><?php echo $row['JoinDate']; ?></td>
                        </tr>
                        </tbody>
                    </table>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormContainer">
                <?php
                if (isset($_GET['modifyCustomers'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('customerResults').style.display='none';
                            document.getElementById('modifyFormContainer').style.display='block';
                            </script>";
                }
                $rowNumber = 0;
                $sql = "SELECT UserID, Email, FirstName, LastName, IsAdmin, JoinDate FROM Users";
                foreach ($db->query($sql) as $row) {
                    $rowNumber++;
                    ?>
                    <form id="modifyForm" name="modifyForm" method="post" action="">
                        <table>
                            <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                            <tr>
                                <th class="userIDCol">ID</label></th>
                                <th class="emailCol"><label for='email'>Email</label></th>
                                <th class="fNameCol"><label for='fname'>Name</label></th>
                                <th class="lNameCol center"><label for='lname'>Surname</label></th>
                                <th class="isAdminCol center"><label for='isAdmin'>Is Admin</label></th>
                                <th class="dateJoinedCol center"><label for='dateJoined'>Join Date</label></th>
                                <th></th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td class="userIDCol"><?php echo $row['UserID']; ?></td>
                                <td class="emailCol"><input type='text' name='email' id='email' class="left"
                                                            value='<?php echo $row['Email']; ?>'/></td>
                                <td class="fNameCol"><input type='text' name='fname' id='fname' class="left"
                                                            value='<?php echo $row['FirstName']; ?>'/></td>
                                <td class="lNameCol"><input type='text' name='lname' id='lname' class="left"
                                                            value='<?php echo $row['LastName']; ?>'/></td>
                                <td class="isAdminCol"><input type='checkbox' name='isAdmin' id='isAdmin'
                                                              value="1" <?php echo($row['IsAdmin'] == 1 ? 'checked="checked"' : "0"); ?>/>
                                </td>
                                <td class="dateJoinedCol"><?php echo $row['JoinDate']; ?></td>
                                <td class="updateBtn"><input type="submit" name="submit" value="Update"/></td>
                                <td class="deleteBtn"><input type='submit' name='submit' value='X' class='red'
                                                             id="deleteButton"></td>
                            </tr>
                            </tbody>
                        </table>
                        <input type='hidden' name='userID' id='userID' value='<?php echo $row['UserID']; ?>'/>
                    </form>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormStatusBar">
                <?php
                if (!empty($modifyFormStatus)) {
                    echo "<script type='text/javascript'>document.getElementById('modifyFormStatusBar').style.display = 'block';</script>";
                    echo '<p>' . implode('<p></p>', $modifyFormStatus) . '</p>';
                    echo "<script type='text/javascript'>clearMessage('modifyFormStatusBar', 2000)</script>";
                }
                // close the database connection
                $db = null;
                ?>
            </div>
        </div>
        <div id="push"></div>
        <?php require 'footer.php' ?>
    </div>
</div>
</body>
</html>